package com.maxjay.component.simplelist.core.parser;

import java.util.List;
import java.util.Map;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Component;

import com.maxjay.component.metadata.model.EntityField;
import com.maxjay.component.simplelist.model.DefaultQueryConditionField;
import com.maxjay.component.simplelist.model.DefaultQueryListField;
import com.maxjay.component.simplelist.model.DefaultQueryScheme;
import com.maxjay.component.simplelist.model.QueryConditionField;
import com.maxjay.component.simplelist.model.QueryScheme;
import com.maxjay.component.simplelist.model.QuerySchemeConfig;
import com.maxjay.system.constant.YesNoConstant;
import com.maxjay.system.service.BaseServiceJdbc;

@Component("querySchemeParser")
public class DatabaseQuerySchemeParser extends BaseServiceJdbc implements QuerySchemeParser {

	@Override
	public QueryScheme parseQueryScheme(String schemeId) {
		try {
			String qrySchemeSql = "select * from T_QRY_SCHEME t where t.OBJ_ID=?";
			Map<String, Object> schemeMap = this.getSimpleJdbcTemplate().queryForMap(qrySchemeSql, schemeId);
			String entityId = (String)schemeMap.get("ENTITY_ID");
			DefaultQueryScheme queryScheme = new DefaultQueryScheme(schemeId, (String)schemeMap.get("NAME"), entityId);
			
			// 解析查询方案配置信息
			QuerySchemeConfig querySchemeConfig = new QuerySchemeConfig();
			String listPageLocation = (String)schemeMap.get("LIST_PAGE_LOCATION");
			String listPluginScript = (String)schemeMap.get("LIST_PLUGIN_SCRIPT");
			QuerySchemeConfig.ViewConfig viewConfig = querySchemeConfig.new ViewConfig(listPageLocation);
			viewConfig.setListPluginScript(listPluginScript);
			querySchemeConfig.setViewConfig(viewConfig);
			queryScheme.setQuerySchemeConfig(querySchemeConfig);
			
			parseQueryConditionField(queryScheme);
			parseQueryListField(queryScheme);
			return queryScheme;
		} catch (EmptyResultDataAccessException e) {
			throw new RuntimeException("无法从T_QRY_SCHEME查找到OBJ_ID为[" + schemeId + "]的查询方案");
		}
	}
	
	/**
	 * 解析查询条件字段
	 * 
	 * @param queryScheme
	 */
	private void parseQueryConditionField(QueryScheme queryScheme) {
		String qryQueryConditionFieldSql = "select * from T_QRY_CONDITION_FIELd t where t.QUERY_SCHEME_ID=?";
		List<Map<String, Object>> conditionFields = this.getSimpleJdbcTemplate().queryForList(qryQueryConditionFieldSql, queryScheme.getId());
		for (Map<String, Object> conditionFieldMap : conditionFields) {
			String entityFieldId = (String)conditionFieldMap.get("ENTITY_FIELD_ID");
			String compareType = (String)conditionFieldMap.get("COMPARE_TYPE");
			QueryConditionField conditionField = new DefaultQueryConditionField(queryScheme.getEntity().getEntityFieldById(entityFieldId), 
					compareType);
			queryScheme.addConditonField(conditionField);
		}
	}
	
	/**
	 * 解析查询列表字段
	 * 
	 * @param queryScheme
	 */
	private void parseQueryListField(QueryScheme queryScheme) {
		String qryQueryListFieldSql = "select * from T_QRY_LIST_FIELd t where t.QUERY_SCHEME_ID=? order by t.ORDER_NO";
		List<Map<String, Object>> listFields = this.getSimpleJdbcTemplate().queryForList(qryQueryListFieldSql, queryScheme.getId());
		for (Map<String, Object> fieldMap : listFields) {
			String entityFieldId = (String)fieldMap.get("ENTITY_FIELD_ID");
			EntityField entityField = queryScheme.getEntity().getEntityFieldById(entityFieldId);
			DefaultQueryListField queryListField = new DefaultQueryListField(entityField);
			if (YesNoConstant.YES.equals((String)fieldMap.get("IS_VIRTUAL"))) {
				queryListField.setIsVirtual(true);
			}
			if (YesNoConstant.NO.equals((String)fieldMap.get("IS_DISPLAY"))) {
				queryListField.setIsDisplay(false);
			}
			queryScheme.addListField(queryListField);
		}
	}

}
